import { defineStore } from 'pinia'

export type Profile = {
  account: string
  nickname: string
  email: string
}

export const useProfileStore = defineStore('profile', {
  state: () => ({}) as Profile,

  actions: {
    getProfile() {
      this.account = sessionStorage.getItem('account') || ''
      this.nickname = sessionStorage.getItem('nickname') || ''
      this.email = sessionStorage.getItem('email') || ''
      return {
        account: this.account,
        nickname: this.nickname,
        email: this.email,
      }
    },
    setProfile(profile: Profile) {
      sessionStorage.setItem('account', profile.account)
      sessionStorage.setItem('nickname', profile.nickname)
      sessionStorage.setItem('email', profile.email)
      this.account = profile.account
      this.nickname = profile.nickname
      this.email = profile.email
    },
    setAccount(account: string) {
      sessionStorage.setItem('account', account)
      this.account = account
    },
    setNickname(nickname: string) {
      sessionStorage.setItem('nickname', nickname)
      this.nickname = nickname
    },
    setEmail(email: string) {
      sessionStorage.setItem('email', email)
      this.email = email
    },
    removeProfile() {
      sessionStorage.removeItem('account')
      sessionStorage.removeItem('nickname')
      sessionStorage.removeItem('email')
      this.account = ''
      this.nickname = ''
      this.email = ''
    },
  },
})
